
from fastapi import FastAPI,APIRouter,Request
from json.decoder import JSONDecodeError
import sys 
from fastapi import UploadFile,Form,File
sys.path.append('..')
from utils import Blogs as F 
from utils.http_response import HttpResponse 
from utils.db import engine
router = APIRouter(prefix="/blogs",tags=["blogs"])
ak = "jBEqfAG6lAUMYqhf9ZuQ"
sk = 'zzh0117.'
from minio import Minio

client = Minio(
    "113.31.110.212:9000",
    secure=False,
    access_key=ak,
    secret_key=sk,
)

@router.get("/get_all")
async def get_all(req:Request):
    resp = HttpResponse(code=200,message="success",data=[])
    resp.data = F.select_all(engine)
    return resp

@router.post("/get_one")
async def get_one(req:Request):
    resp = HttpResponse(code=200,message="success",data=[])
    try:
        body = await req.json()
    except Exception as JSONDecodeError:
        resp.message = "json decode error"
        resp.code = 500
        print(JSONDecodeError)
        return resp 
    resp.data = F.select_by_col(engine,body)
    return resp

@router.post("/get_item_in_list")
async def get_item_in_list(req:Request):
    resp = HttpResponse(code=200,message="success",data=[])
    try:
        body = await req.json()
    except Exception as JSONDecodeError:
        resp.message = "json decode error"
        resp.code = 500
        return resp
    resp.data = F.select_by_list_of_col(engine,body)
    return resp

@router.post("/update_one")
async def update_one(req:Request):
    resp = HttpResponse(code=200,message="success",data=[])
    try:
        body = await req.json()
    except Exception as JSONDecodeError:
        resp.message = "json decode error"
        resp.code = 500
        return resp 
    resp.data = F.update_one(engine,body["cond"],body["item"])  
    return resp 

@router.post("/delete_one")
async def delete_one(req:Request):
    resp = HttpResponse(code=200,message="success",data=[])
    try:
        body = await req.json()
    except Exception as JSONDecodeError:
        resp.message = "json decode error"
        resp.code = 500
        return resp 
    resp.data = F.delete_one(engine,body)  
    return resp 



@router.post("/add_one")
async def add_one(req:Request):
    resp = HttpResponse(code=200,message="success",data=[])
    try:
        body = await req.json()
    except Exception as JSONDecodeError:
        resp.message = "json decode error"
        resp.code = 500
        return resp 
    resp.data = F.add_one(engine,body)  
    return resp 


@router.post("/upload_img")
async def upload_img(req:Request):
    form_data = await req.form()
    # name,file = form_data
    print(form_data)
    uploaded_file = form_data['file']
    filename = uploaded_file.filename
    contents = await uploaded_file.read()
    bucket_name = "foodsite"
    with open('./tmp/'+filename,'wb') as f:
        f.write(contents)
    try:
        client.fput_object(bucket_name,filename,'./tmp/'+filename)
    except Exception  as e : 
        print(e)
    return {"code":200,"message":"success","data":f"http://113.31.110.212:9000/foodsite/{filename}"}
    
